Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Code returns [4, 5, 6, 7, 8] instead of [4, 5, 7]. The loop doesn't acknowledge that [6] and [8] are in variable statues

function solution(statues) {
  statues.sort()
  let fullArray = [];
  let missingStatues = [];
  for (let i = Math.min(...statues); i <= Math.max(...statues); i++) {
    fullArray.push(i);
  }
  for (let i = 0; i < fullArray.length; i++) {
    if (!(fullArray[i] in statues)) {
      missingStatues.push(fullArray[i]);
    }
  }
  let missingStatuesCount = missingStatues.length;
  return missingStatues;
}

console.log(solution([6, 2, 3, 8]))

I was expecting [4, 5, 7] but I got [4, 5, 6, 7, 8]. The loop that pushes fullArray[i] into missingStatues, is also pushing [6] and [8], which are in statues and should not be pushed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function solution(statues) {
    statues.sort()

The default comparisons made by the .sort() method are string comparisons. Your arrays contain numbers, apparently, so you need a numeric comparator function:

    statues.sort((a, b) => a - b);

Then:

    for (let i=Math.min(...statues);i<=Math.max(...statues);i++) {
        fullArray.push(i);
    }

Once you have successfully sorted the array, statues[0] will be the smallest value and statues[statues.length - 1] will be the largest. There is no need to copy the array and call Math.min() or Math.max().

    for (let i=0;i<fullArray.length;i++) {
        if (!(fullArray[i] in statues)) {
            missingStatues.push(fullArray[i]);
        } 
    }

The in operator is for checking object keys, not values. What you're looking for is the .includes() method:

        if (!statues.includes(fullArray[i]))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!